home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Science / µSim 1.0b5 folder / source / Animation.c < prev    next >
Encoding:
Text File  |  1994-10-01  |  7.8 KB  |  298 lines  |  [TEXT/MMCC]

  1. /*
  2. Copyright © 1993,1994 by Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware: you can copy, exchange, modify this
  5. code as you wish. You may include this code in any kind of application: freeware,
  6. shareware, or commercial, provided that full credits are given.
  7. You may not sell or distribute this code for profit.
  8. */
  9.  
  10. //#pragma load "MacDump"
  11.  
  12. #include    "UtilsSys7.h"
  13. #include    "Globals.h"
  14. #include    "Animation.h"
  15. #include    "ControlStore.h"
  16. #include    "DoEditDialog.h"
  17. #include    "DoMenu.h"
  18. #include    "Main.h"
  19. #include    "Registers.h"
  20. #include    "Conversions.h"
  21.  
  22. #if defined(FabSystem7orlater)
  23.  
  24. //#pragma segment Main
  25.  
  26. GrafPtr    offScr;        /* offscreen GrafPort for animation */
  27. PicHandle    images[kLAST_PICT - kFIRST_PICT + 1];    /* Handles to Pictures */
  28. Rect    rval[kP_MIR - kP_ALATCH + kMIRSubboxes];    /* in these rects we write the values */
  29. RgnHandle    mirUpdRgn;
  30.  
  31. static Boolean PtInObj(Point, RectPtr, short obj);
  32.  
  33. /* PtInObj: finds out if thePt is in the obj object, returning the object
  34. Rect in neededRect */
  35.  
  36. static Boolean PtInObj(Point thePt, RectPtr neededRect, short obj)
  37. {
  38. register PicHandle    tempPH;
  39.  
  40. tempPH = images[obj];
  41. if (*tempPH == nil)
  42.     LoadResource((Handle)tempPH);
  43. *neededRect = (*tempPH)->picFrame;
  44. neededRect->right--;
  45. neededRect->bottom--;
  46. return(PtInRect(thePt, neededRect));
  47. }
  48.  
  49. /* UpdateMir: takes care of updating the mir image in the Animation window */
  50.  
  51. void UpdateMir(un_mir newmir)
  52. {
  53. Str255    tempS;
  54. GrafPtr    savePort;
  55. register RectPtr    RectScan = &rval[kP_MIR - kP_ALATCH];
  56.  
  57. GetPort(&savePort);
  58. SetPort(offScr);
  59. MyNumToString((newmir).bits.amux, tempS);
  60. TextBox(&tempS[1], Length(tempS), RectScan++, teJustCenter);
  61. MyNumToString((newmir).bits.cond, tempS);
  62. TextBox(&tempS[1], Length(tempS), RectScan++, teJustCenter);
  63. MyNumToString((newmir).bits.alu, tempS);
  64. TextBox(&tempS[1], Length(tempS), RectScan++, teJustCenter);
  65. MyNumToString((newmir).bits.shift, tempS);
  66. TextBox(&tempS[1], Length(tempS), RectScan++, teJustCenter);
  67. MyNumToString((newmir).bits.mbr, tempS);
  68. TextBox(&tempS[1], Length(tempS), RectScan++, teJustCenter);
  69. MyNumToString((newmir).bits.rd, tempS);
  70. TextBox(&tempS[1], Length(tempS), RectScan++, teJustCenter);
  71. MyNumToString((newmir).bits.wr, tempS);
  72. TextBox(&tempS[1], Length(tempS), RectScan++, teJustCenter);
  73. MyNumToString((newmir).bits.mar, tempS);
  74. TextBox(&tempS[1], Length(tempS), RectScan++, teJustCenter);
  75. MyNumToString((newmir).bits.dsc, tempS);
  76. TextBox(&tempS[1], Length(tempS), RectScan++, teJustCenter);
  77. ShortToHexString((newmir).bits.a, tempS);
  78. TextBox(&tempS[4], 1, RectScan++, teJustCenter);
  79. ShortToHexString((newmir).bits.b, tempS);
  80. TextBox(&tempS[4], 1, RectScan++, teJustCenter);
  81. ShortToHexString((newmir).bits.c, tempS);
  82. TextBox(&tempS[4], 1, RectScan++, teJustCenter);
  83. MyNumToString((newmir).bits.map, tempS);
  84. TextBox(&tempS[1], Length(tempS), RectScan++, teJustCenter);
  85. MyNumToString((newmir).bits.addr, tempS);
  86. TextBox(&tempS[1], Length(tempS), RectScan++, teJustRight);
  87. SetPort(gWPtr_Animation);
  88. InvalRgn(mirUpdRgn);
  89. SetPort(savePort);
  90. }
  91.  
  92. /* ChangedBox: does all the housekeeping when an on-screen object is modified */
  93.  
  94. void ChangedBox(short obj)
  95. {
  96. Str255    tempS;
  97. GrafPtr    savePort;
  98.  
  99. GetPort(&savePort);
  100. SetPort(offScr);
  101. ShortToHexString(gParts[obj], tempS);
  102. if((obj >= kP_INCR - kFIRST_PICT)&&(obj <= kP_MPC - kFIRST_PICT)) {
  103.     *(Byte *)(&gParts[obj]) = 0;
  104.     MyNumToString(gParts[obj], tempS);
  105.     }
  106. else {
  107.     if( obj == kP_ALU - kFIRST_PICT ) {
  108.         register unsigned char *mslstring = (unsigned char *)&tempS + 8;
  109.         register StringPtr ZeroandOne = *GetString(kSTR_BINALLOWED);
  110.  
  111.         *mslstring++ = (gParts[kP_ALU - kFIRST_PICT] < 0 ? *(ZeroandOne+2) : *(ZeroandOne+1));
  112.         *mslstring++ = 13;
  113.         *mslstring++ = (gParts[kP_ALU - kFIRST_PICT] == 0 ? *(ZeroandOne+2) : *(ZeroandOne+1));
  114.         TextSize(9);
  115.         TextBox((unsigned char *)&tempS + 8, 3, &rval[kP_MSL - kFIRST_PICT], teJustCenter);
  116.         TextSize(12);
  117.         }
  118.     }
  119. TextBox(&tempS[1], Length(tempS), &rval[obj], teJustCenter);
  120. SetPort(gWPtr_Animation);
  121. InvalRect(&rval[obj]);
  122. if (obj == kP_ALU - kFIRST_PICT)
  123.     InvalRect(&rval[kP_MSL - kFIRST_PICT]);
  124. SetPort(savePort);
  125. }
  126.  
  127. void ChangedAllBoxes(void)
  128. {
  129. register short    i;
  130.  
  131. for (i = kP_ALATCH - kFIRST_PICT; i <= kP_ALU - kFIRST_PICT; ChangedBox(i++))
  132.     ;
  133. }
  134.  
  135. /* ActivateObjs: activates (blackens) a list of objects */
  136.  
  137. void ActivateObjs(const short *objs)
  138. {
  139. Rect    tempRect;
  140. GrafPtr    savePort;
  141. register short    pos;
  142.  
  143. GetPort(&savePort);
  144. SetPort(offScr);
  145. PenNormal();
  146.  
  147. for( ; pos = *objs++; ) {
  148.     register PicHandle    tempPicH;
  149.     
  150.     tempPicH = images[pos - kFIRST_PICT];
  151.     if( *tempPicH == nil ) /* Resource purged */
  152.         LoadResource((Handle)tempPicH);
  153.     tempRect = (*tempPicH)->picFrame;
  154.     SetPort(offScr);
  155.     DrawPicture(tempPicH, &tempRect);
  156.     SetPort(gWPtr_Animation);
  157.     InvalRect(&tempRect);
  158.     }
  159. SetPort(savePort);
  160. }
  161.  
  162. /* DeactivateObjs: deactivates (grays) a list of objects */
  163.  
  164. void DeactivateObjs(const short *objs)
  165. {
  166. Rect    tempRect;
  167. GrafPtr    savePort;
  168. register RgnHandle    tempRgn = NewRgn();
  169. register RgnHandle    tempRgn2 = NewRgn();
  170. register short    pos;
  171.  
  172. GetPort(&savePort);
  173. SetPort(offScr);
  174. PenPat(&qd.gray);
  175. PenMode(notPatBic);
  176.  
  177. for( ; pos = *objs++; ) {
  178.     register PicHandle    tempPicH;
  179.     
  180.     tempPicH = images[pos - kFIRST_PICT];
  181.     if( *tempPicH == nil ) /* Resource purged */
  182.         LoadResource((Handle)tempPicH);
  183.     tempRect = (*tempPicH)->picFrame;
  184.     SetPort(offScr);
  185.     DrawPicture(tempPicH, &tempRect);
  186.     tempRect.right--;
  187.     tempRect.bottom--;
  188.     if(pos <= kP_MIR) {
  189.         if(pos == kP_MIR) {
  190.             CopyRgn(mirUpdRgn, tempRgn2);
  191.             InvertRgn(mirUpdRgn);
  192.             }
  193.         else
  194.             RectRgn(tempRgn2, &rval[pos - kP_ALATCH]);
  195.         RectRgn(tempRgn, &tempRect);
  196.         DiffRgn(tempRgn, tempRgn2, tempRgn);
  197.         }
  198.     else
  199.         RectRgn(tempRgn, &tempRect);
  200.     PaintRgn(tempRgn);
  201.     SetPort(gWPtr_Animation);
  202.     InvalRect(&tempRect);
  203.     }
  204. DisposeRgn(tempRgn2);
  205. DisposeRgn(tempRgn);
  206. SetPort(savePort);
  207. }
  208.  
  209. /* Update_Animation: handler for the update event */
  210.  
  211. void Update_Animation(WindowPtr w)
  212. {
  213. //Str15    tempS;
  214.  
  215. CopyBits(&offScr->portBits, &w->portBits, &offScr->portBits.bounds,
  216.             &w->portRect, srcCopy, nil);
  217.  
  218. /*
  219. MyNumToString(timingInfo, tempS);
  220. MoveTo(40,40);
  221. DrawString(&tempS);
  222. */
  223. }
  224.  
  225. /* Do_Animation: someone has clicked in content … */
  226.  
  227. void Do_Animation(WindowPtr /*w*/, EventRecord *theEvent)
  228. {
  229. //#pragma unused (w)
  230.  
  231. Rect    tempRect;
  232. Point    myPt;
  233. register short    code;
  234.  
  235. myPt = theEvent->where;
  236. GlobalToLocal(&myPt);
  237. if (PtInObj(myPt, &tempRect, code = kP_ALATCH - kFIRST_PICT) ||
  238.     PtInObj(myPt, &tempRect, code = kP_BLATCH - kFIRST_PICT) ||
  239.     PtInObj(myPt, &tempRect, code = kP_MAR - kFIRST_PICT) ||
  240.     PtInObj(myPt, &tempRect, code = kP_MBR - kFIRST_PICT) ||
  241.     PtInObj(myPt, &tempRect, code = kP_AMUX - kFIRST_PICT) ||
  242.     PtInObj(myPt, &tempRect, code = kP_ALU - kFIRST_PICT) ||
  243.     PtInObj(myPt, &tempRect, code = kP_SHIFTER - kFIRST_PICT) ||
  244.     PtInObj(myPt, &tempRect, code = kP_MMUX - kFIRST_PICT) ||
  245.     PtInObj(myPt, &tempRect, code = kP_INCR - kFIRST_PICT) ||
  246.     PtInObj(myPt, &tempRect, code = kP_MPC - kFIRST_PICT) ||
  247.     PtInObj(myPt, &tempRect, code = kP_REGISTERS - kFIRST_PICT) ||
  248.     (PtInObj(myPt, &tempRect, code = kP_CONTSTORE - kFIRST_PICT) && DocIsOpen) ) {
  249.     if (TrackObject(&tempRect)) {
  250.         if (code == kP_REGISTERS - kFIRST_PICT)
  251.             DoMenuWindows(kMItem_Registers);
  252.         else if (code == kP_CONTSTORE - kFIRST_PICT)
  253.             DoMenuWindows(kMItem_Microprogram);
  254.         else {
  255.             (void)DoEditDialog(code, kPART_NAMES,
  256.                                 (code >= kP_INCR - kFIRST_PICT) &&
  257.                                     (code <= kP_MPC - kFIRST_PICT) ? kPOP_DEC : kPOP_HEX);
  258.             UnloadSeg(DoEditDialog);
  259.             }
  260.         }
  261.     }
  262. }
  263.  
  264. /* TrackObject: like all the TrackThing in the Mac OS */
  265.  
  266. Boolean    TrackObject(RectPtr    r)
  267. {
  268. Point    myPt;
  269. register Boolean    inrect;
  270.  
  271. InvertRect(r);
  272. inrect = true;
  273. do {
  274.     register Boolean tempB;
  275.  
  276.     GetMouse(&myPt);
  277.     if ((tempB = PtInRect(myPt, r)) != inrect) {
  278.         InvertRect(r);
  279.         inrect = tempB;
  280.         }
  281.     }
  282. while( StillDown() );
  283. if (inrect) {
  284.     InvertRect(r);
  285.     }
  286. return inrect;
  287. }
  288.  
  289. /* procedure called when closing the Animation window */
  290.  
  291. void CloseAnimation(WindowPtr w)
  292. {
  293. DoCloseWindow(w, kMItem_Animation);
  294. }
  295.  
  296. #endif
  297.  
  298.